import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:get/get.dart';
import 'package:sgy_flutter/ble/ble_manager.dart';
import 'package:sgy_flutter/extensions/get.dart';
import 'package:sgy_flutter/gen/assets.gen.dart';
import 'package:sgy_flutter/model/device_info.dart';
import 'package:sgy_flutter/model/product_type.dart';
import 'package:sgy_flutter/page/detail/widget/ble_connection_view.dart';
import 'package:sgy_flutter/page/detail/widget/signal_view.dart';
import 'package:sgy_flutter/widget/app_dialog.dart';

import '../logic.dart';
import '../widget/ble_state_view.dart';

class AxoTopView extends GetView<DetailLogic> {
  const AxoTopView({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        border: Border(bottom: BorderSide.none),
        color: Get.theme.primaryColor,
      ),
      padding: EdgeInsets.only(top: 20, bottom: 10, left: 26, right: 26),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          IntrinsicHeight(
            child: Row(
              children: [
                Text(
                  "${controller.bleManager.deviceModel?.productType.name.toUpperCase().replaceAll("CC", "cc")}",
                  style: TextStyle(
                    fontSize: 38,
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Container(
                  color: Colors.white,
                  height: double.infinity,
                  width: 2,
                  margin: EdgeInsets.symmetric(horizontal: 10, vertical: 4),
                ),
                Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      "POWER METER",
                      style: TextStyle(
                          fontSize: 14,
                          color: Colors.white,
                          fontWeight: FontWeight.bold),
                    ),
                    Text(
                      "硬件版本".tr +
                          " ${controller.bleManager.deviceModel?.hardware}",
                      style: TextStyle(
                          fontSize: 14,
                          color: Colors.white,
                          fontWeight: FontWeight.bold),
                    ).marginOnly(top: 6),
                  ],
                ),
              ],
            ),
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  FilledButton(
                    style: FilledButton.styleFrom(
                        backgroundColor: Colors.white,
                        foregroundColor: Colors.black,
                        padding:
                            EdgeInsets.symmetric(vertical: 4, horizontal: 8),
                        minimumSize: Size.zero,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(6))),
                    onPressed: () async {
                      if (controller.bleManager.connectionState.value ==
                          BluetoothConnectionState.connected) {
                        final disconnect = await Get.dialog(
                            AppDialog(
                              title: "提示".tr,
                              message: "断开连接？".tr,
                              submit: "确定".tr,
                              cancel: "取消".tr,
                              onSubmit: () => Get.backLegacy(result: true),
                            ),
                            barrierDismissible: false);
                        if (disconnect == true) {
                          controller.disconnect();
                        }
                      }
                      if (controller.bleManager.connectionState.value ==
                          BluetoothConnectionState.disconnected) {
                        controller.connect();
                      }
                    },
                    child: BleConnectionView(
                      bleManager: controller.bleManager,
                      builder: (state) => Text(
                        state == BluetoothConnectionState.connected
                            ? '断开连接'.tr
                            : state == BluetoothConnectionState.disconnected
                                ? '连接'.tr
                                : '连接中'.tr,
                        style: TextStyle(fontSize: 12),
                      ),
                    ),
                  ),
                  Text(
                    "ID:${controller.bleManager.deviceModel?.deviceID}",
                    style: TextStyle(fontSize: 12, color: Colors.white),
                  ),
                ],
              ),
              Spacer(),
              SignalView(
                bleManager: controller.bleManager,
                showDeviceId: false,
              ),
            ],
          ).marginOnly(top: 20)
        ],
      ),
    );
  }
}
